<?php
//自动生成,请根据需要修改
namespace app\user\controller;
use app\common\controller\Base;
use tools\curl\Curl;
use Think\Db;
/**
* @title 管理员信息页面（模块01-01）
* @description 管理员信息说明
* @group 用户中心（01）
* @header name:model_id require:0 default:01-01 desc:模块
* @groupremark 已使用错误码：1-01-01-0001 校验失败 1-01-01-0002 添加失败 1-01-01-0003 编辑校验失败 1-01-01-0004 编辑失败 1-01-01-0005 获取详情失败  1-01-01-0006 获取列表失败 1-01-01-0007获取单个详情失败 1-01-01-0008 删除失败
*/
class AdminUser extends Base{
		//测试
	public function test(){
		return 'test 111';
	}
	
	/**
	* @title 添加管理员信息
	* @description 添加管理员信息
	* @author 宋晓文
	* @url /user/admin_user/add
	* @method *
	*
	* @header name:token require:1 default: desc:token
	*
	* @param name:username type:varchar require:0 other: desc:登录名
	* @param name:password type:varchar require:0 other: desc:密码
	* @param name:salt type:varchar require:0 other: desc:盐值
	* @param name:nickname type:varchar require:0 other: desc:昵称
	* @param name:head_img type:varchar require:0 other: desc:头像地址
	* @param name:islock type:tinyint require:0 other: desc:锁定（0正常；1锁定）
	*@return data:管理员信息@!
	* @data 	id:id	username:登录名	password:密码	salt:盐值	nickname:昵称	head_img:头像地址	islock:锁定（0正常；1锁定）
	*@return error_id:1-01-01-0001 校验失败  1-01-01-0002 添加失败
	*/
	public function add(){
        $input=input();
        $rule = [
            'account_name' => ['require'],
            'nickname' => ['require'],
            'password' => ['require'],
            'shop_role_id' => ['require','number']
        ];
        $vmsg = [
            'require' => '不能为空',
            'number' => '必须是数字'
        ];
        $validate = new \think\Validate($rule,$vmsg);
        $result = $validate->batch()->check($input);
        if ($result !== true) {
            $msg['msg']='校验失败';
            $msg['validate']=$validate->getError();
            return $this->errorJson($msg);
        }
        $data_account = [
            'token'=>$input['token'],
            'account_name'=>$input['account_name'],
            'password'=>md5($input['password'])
        ];
        $account = get_curl('login','account/add',$data_account);

        if (!$account['status']){
            $msg['msg']=$account['msg'];
            $msg['data']=$account;
            return $this->errorJson($msg);
        }

        $data_role = [
            'token'=>$input['token'],
            'account_id' => $account['account_id'],
            'shop_role_id' => $input['shop_role_id']
        ];
        get_curl('shop_admin_auth','admin_role/add_admin_role',$data_role);

        $data_admin_user = [
            'account_id' => $account['account_id'],
            'nickname' => $input['nickname'],
            'head_img' => empty($input['head_img'])?'':$input['head_img'],
        ];
		$model=new \app\user\model\AdminUser();
		$flag = $model->insert($data_admin_user);


		if($flag){
			$msg['msg']='添加成功';
			return $this->successJson($msg);
		}else{
			$msg['error_id']='1-01-01-0002';
			$msg['msg']='添加失败';
			return $this->errorJson($msg);
		}
	}

	/**
	* @title 编辑管理员信息
	* @description 编辑管理员信息
	* @author 宋晓文
	* @url /user/admin_user/edit
	* @method *
	*
	* @header name:token require:1 default: desc:token
	*
    * @param name:id type:int require:0 other: desc:id
    * @param name:account_id type:int require:0 other: desc:account_id
    * @param name:status type:tinyint require:0 other: desc:状态（0-禁用；1-正常）
    * @param name:shop_role_id type:tinyint require:0 other: desc:shop_role_id
    * @return data:管理员信息@!
	* @data 	id:id	username:登录名	password:密码	salt:盐值	nickname:昵称	head_img:头像地址	islock:锁定（0正常；1锁定）
	* @return error_id:1-01-01-0003 校验失败 1-01-01-0004 修改失败
	*/
	public function edit(){
        $input=input();
        $rule = [
            'id' => ['require','number'],
            'account_id' => ['require','number'],
            'status' => ['require','number'],
            'shop_role_id' => ['require','number'],
        ];
        $vmsg = [
            'require' => '不能为空',
            'number' => '必须是数字'
        ];
        $validate = new \think\Validate($rule,$vmsg);
        $result = $validate->batch()->check($input);
        if ($result !== true) {
            $msg['msg']='校验失败';
            $msg['validate']=$validate->getError();
            return $this->errorJson($msg);
        }
        $data['id'] = $input['id'];
        $data['status'] = $input['status'];

        $admin_user = Db::name('admin_user')->find($data['id']);
        if ($admin_user['status'] != $data['status']){
            $flag = Db::name('admin_user')->where('id','=',$data['id'])->update($data);
        }else{
            $flag =true;
        }


        $data_role['token'] = $input['token'];
        $data_role['account_id'] = $input['account_id'];
        $data_role['shop_role_id'] = $input['shop_role_id'];
        $role = get_curl('shop_admin_auth','admin_role/edit_admin_role',$data_role);

        if($flag){
            $msg['msg'] = '修改成功';
            $msg['data'] = $role;
            return $this->successJson($msg);
        }else{
            $msg['error_id']='1-01-01-0002';
            $msg['msg']='修改失败';
            return $this->errorJson($msg);
        }
	}
		
	/**
	* @title 删除管理员信息
	* @description 删除管理员信息
	* @author 宋晓文
	* @url /user/admin_user/delete
	* @method *
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:id type:int|array require:1 other: desc:要删除的id
	*@return error_id:1-01-01-0008 删除失败
	*/
	public function delete(){
		$id=input('id');
		$model=new \app\user\model\AdminUser();
		$flag = $model->destroy($id);
		if($flag){
			$msg['msg']='删除成功';
			return $this->successJson($msg);
		}else{
			$msg['msg']='删除失败';
			$msg['error_id']='1-01-01-0008';
			return $this->errorJson($msg);
		}
	}
	    
    /**
    * @title 单个查询管理员信息
    * @description 单个查询管理员信息
    * @author 宋晓文
    * @url /user/admin_user/info
    * @method *
    *
    * @param name:token require:1 default: desc:token
    *
        * @param name:id type:int require:0 other: desc:id
        * @param name:account_id type:int require:0 other: desc:account_id
        * @param name:username type:varchar require:0 other: desc:登录名
        * @param name:password type:varchar require:0 other: desc:密码
        * @param name:salt type:varchar require:0 other: desc:盐值
        * @param name:nickname type:varchar require:0 other: desc:昵称
        * @param name:head_img type:varchar require:0 other: desc:头像地址
        * @param name:islock type:tinyint require:0 other: desc:锁定（0正常；1锁定）
        *@return data:管理员信息@!
    * @data 	id:id	username:登录名	password:密码	salt:盐值	nickname:昵称	head_img:头像地址	islock:锁定（0正常；1锁定）
    *@return error_id:1-01-01-0005 校验失败 1-01-01-0007 获取失败
    */
    public function info(){
        $model=new \app\user\model\AdminUser();
        $Fields=$model->getTableFields();
        $data=\think\facade\Request::only($Fields);
        /**
        *$validate = new \app\user\validate\AdminUser();
        *$validate->scene('info');
        *$result = $validate->batch(true)->check($data);
        */
        if (!$data) {
            $msg['msg']='必须有参数';
            $msg['error_id']='1-01-01-0005';
            return $this->errorJson($msg);
        }
        $model=new \app\user\model\AdminUser();
        $info = $model->where($data)->find();
        if($info){
            $msg['msg']='获取成功';
            $msg['data']=$info;
            return $this->successJson($msg);
        }else{

            $msg['msg']='获取失败';
            $msg['error_id']='1-01-01-0007';
            return $this->errorJson($msg);
        }
    }
		
	/**
	* @title 查询列表管理员信息
	* @description 查询列表管理员信息
	* @author 宋晓文
	* @url /user/admin_user/get_list
	* @method *
	* @param name:token require:1 default: desc:token
	* @param name:page require:1 default: desc:第几页
	* @param name:pagesize require:1 default: desc:每页数
	* @return list:管理员信息@
	* @list 	id:id	username:登录名	password:密码	salt:盐值	nickname:昵称	head_img:头像地址	islock:锁定（0正常；1锁定）
	*@return error_id:1-01-01-0006 校验失败
	*/
	public function get_list(){
		$model=new \app\user\model\AdminUser();
		$Fields=$model->getTableFields();
		$data=\think\facade\Request::only($Fields);

        foreach ($data as $k=>$v){
            if ($v==''){
                unset($data[$k]);
            }
        }
		$list = $model->where($data)->paginate();
		foreach ($list as $k=>$v){
		    $list[$k]['shop_role_id'] = 0;
            $list[$k]['role_name'] = null;
            $list[$k]['role'] = null;
            $list[$k]['account_name'] = null;
            $role = get_curl('shop_admin_auth','admin_role/role_by_account_id',['account_id'=>$v['account_id'],'token'=>input('token')]);
            if ($role['status']){
                $list[$k]['shop_role_id'] = $role['data']['shop_role_id'];
                $list[$k]['role_name'] = $role['data']['name'];
                $list[$k]['role'] = $role['data']['role'];
            }
            $account = get_curl('login','account/get_name_by_account_id',['id'=>$v['account_id'],'token'=>input('token')]);
            $list[$k]['account'] = $account;
            if ($account['status']){
                $list[$k]['account_name'] = $account['data']['account_name'];
            }
        }

		$msg['list']=$list;
		return $this->successJson($msg);
	}

    /**
     * @title 后台通过token获取用户信息
     * @description 后台通过token获取用户信息
     * @author 李锐志
     * @url /user/admin_user/info_by_token
     * @method *
     * @param name:token require:1 default: desc:token
     */
    public function info_by_token()
    {
        $data = input();

        $result = Db::name('admin_user')
            ->where('account_id', ACCOUNTID)
            ->where('status', 1)
            ->find();

        //调用管理员权限模块，获取管理员的角色
        $admin_auth_model_api = Config('model_api.shop_admin_auth');
        $url = $admin_auth_model_api."/admin_role/role_by_token";
        $role_list = Curl::post($url, array('token'=> $data['token']));

        $role_list = Curl::json2arr($role_list);
        if(!$role_list['status']){
            $msg['role_list']=[];
            $msg['msg']='获取角色失败';
            $msg['error_id']='1-02-02-0003';
            $msg['url']=$url;
            return $this->errorJson($msg);
        }
        $msg['role_list']=$role_list;
        $role_list=$role_list['list'];

        $roles=[];
        if (is_array($role_list)) {
            foreach ($role_list as $role){
                $roles[]=$role['role'];
            }
        }

        //获取角色数据
        $msg['roles'] = $roles;


        if ($result) {
            $msg['msg'] = '用户信息获取成功';
            $msg['data'] = $result;
            return $this->successJson($msg);
        } else {
            $msg['msg'] = '用户信息获取失败';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 获取密码
     * @description 后台通过token获取用户信息
     * @author 李锐志
     * @url /user/admin_user/get_pwd
     * @method *
     * @param name:token require:1 default: desc:token
     */
    public function get_pwd(){
        $msg['data'] = substr(str_shuffle('abcedfghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%'), 0, 16);
        return $this->successJson($msg);
    }


}
